package ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import service.IUserService;
import service.UserService;
import model.User;
public class RegisterDialog extends JDialog implements ActionListener{
	private JLabel label,label1,label2;
	private JTextField Account;
	private JPasswordField Password1,Password2;
	private JButton register,cancel;
	private JPanel p1;
	public RegisterDialog(){
		label=new JLabel(" 用户名 ：");
		label1=new JLabel("  密码  ：");
		label2=new JLabel("确定密码：");
		Account=new JTextField(15);
		Password1=new JPasswordField(15);
		Password2=new JPasswordField(15);
		register=new JButton("注册");
		cancel=new JButton("取消");
		p1 = (JPanel)this.getContentPane();
		p1=new JPanel();
		p1.add(label);p1.add(Account);//输入用户名
		p1.add(label1);p1.add(Password1);//输入密码
		p1.add(label2);p1.add(Password2);//再次确定输入密码
		p1.add(register);//注册
		p1.add(cancel);//取消

		label.setBounds(80,40,100,30);
		label.setFont(new Font("宋体", Font.PLAIN, 20));
		label.setForeground(Color.BLACK);
		Account.setBounds(180,40,165,30);

		label1.setBounds(80,75,100,30);
		label1.setFont(new Font("宋体", Font.PLAIN, 20));
		label1.setForeground(Color.BLACK);
		Password1.setBounds(180, 75, 165, 30);

		label2.setBounds(80,115,100,30);
		label2.setFont(new Font("宋体", Font.PLAIN, 20));
		label2.setForeground(Color.BLACK);
		Password2.setBounds(180, 115, 165, 30);

		register.setBounds(135,175,75,35);
		register.setFont(new Font("宋体", Font.PLAIN, 20));
		register.setForeground(Color.BLUE);
		cancel.setBounds(225, 175, 75,35);
		cancel.setFont(new Font("宋体", Font.PLAIN, 20));
		cancel.setForeground(Color.BLUE);
		
		this.add(p1);
		setTitle("新用户注册界面");
		setBounds(100,100,450, 300);
		getContentPane().setLayout(new BorderLayout());
		p1.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(p1, BorderLayout.CENTER);
		p1.setLayout(null);
		setVisible(true);
		setResizable(false);

		register.addActionListener(this);
		cancel.addActionListener(this);
		
		Account.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){//当文本框中按下鼠标时，将文本框的内容全部清除
				Account.setText("");
			}
		});
		Account.addFocusListener( new FocusAdapter(){ 
			 public void focusLost(FocusEvent e)
			{
				 try {
					IUserService service=new UserService();
					if(service.IsName(Account.getText())){
						JOptionPane.showMessageDialog(null, "此用户名已经存在，请再次输入新的用户名！");
						Account.setText(null);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		 });

		 Password2.addFocusListener( new FocusAdapter(){ 
			 public void focusLost(FocusEvent e)
			{
				 String p1=String.valueOf(Password1.getPassword());
				 String p2=String.valueOf(Password2.getPassword());
				 if(!p1.equals(p2)){
					 JOptionPane.showMessageDialog(null, "两次输入的密码不一致，请再次确认密码！");
					 Password2.setText("");
				 }
			}
		 });
	}

	public static void main(String[] args) {
		 new RegisterDialog();
	}
	public void actionPerformed(ActionEvent e) {
		UserService service=null;
		User user = new User();
		String p1=String.valueOf(Password1.getPassword());
		 String p2=String.valueOf(Password2.getPassword());
		if(e.getSource()==register){
			try{
				 service = new UserService();
			       if(p1.equals(p2)==true){
			    	   user.setId(user.getId());
			    	   user.setName(Account.getText());
			    	   user.setPassword(p1);
			    	   if(service.insertUser(user)){
			    	       JOptionPane.showMessageDialog(null, "新用户注册成功！","注册成功",JOptionPane.INFORMATION_MESSAGE);
			    	       setVisible(false);
			    	       new LoginDialog();
			    	       }
			    	   else{
			   				setVisible(false);
			   				JOptionPane.showMessageDialog(null, "创建用户失败","提示",JOptionPane.INFORMATION_MESSAGE);
			   			   new RegisterDialog();
			    	   }    
			       }
			}catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		if(e.getSource()==cancel){
			setVisible(false);
			new LoginDialog();
		}
	}
}

